//
//  CSSComposeTooBar.swift
//  weibo28
//
//  Created by chushushan on 16/5/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

enum CSSComposeTooBarButtonType: Int {

    //图片
    case Picture = 0
    //@
    case Mention = 1
    //话题
    case Trend = 2
    //  表情
    case Emoticon = 3
    //  add
    case Add = 4
   
}





//只是一个容器，不具备渲染效果
class CSSComposeTooBar: UIStackView {

    
    //设置闭包.传递按钮点击事件
    var didSeletedButtonClosure: ((type: CSSComposeTooBarButtonType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
    
        //指定布局方式
        axis = .Horizontal
        //填充方式.所有子控件一样宽高
        distribution = .FillEqually
        
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: .Add)
    
    }
    
    
    //添加子button
    private func addChildButton(imageName: String, type: CSSComposeTooBarButtonType) {
    
        let  button = UIButton()
        //获取枚举原始值
        button.tag = type.rawValue
        
        //点击事件
        button.addTarget(self, action: "buttonClikc:", forControlEvents: .TouchUpInside)
        
        
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        //去掉高亮状态
        button.adjustsImageWhenHighlighted = false
        
        addArrangedSubview(button)
    }
    
    //按钮的点击事件
    @objc private func buttonClikc(button: UIButton) {
    
        //把值转成枚举类型
        let type = CSSComposeTooBarButtonType(rawValue: button.tag)!
    
        //调用闭包
        didSeletedButtonClosure?(type: type)
    }
    
    
    
    
    

}
